package com.jdkreader.io;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * IO 管道实例程序
 */
public class PipeExample {

    public static void main(String[] args){

        PipeExample pipeExample=new PipeExample();

        PipeExample.Sender sender=pipeExample.new Sender();
        PipeExample.Receiver receiver=pipeExample.new Receiver();

        PipedInputStream in=receiver.getInputStream();
        PipedOutputStream out =sender.getOutputStream();

        try {
            in.connect(out);
            sender.start();
            receiver.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 管道输出流测试
     */
    class Sender extends Thread{

        //定义一个管道输出流，需要绑定到一个管道输入流才能正常工作
        private PipedOutputStream out=new PipedOutputStream();
        // 获得“管道输出流”对象
        public PipedOutputStream getOutputStream(){
            return out;
        }
        @Override
        public void run() {
            //writeShortMessage();
            writeLongMessage();
        }

        /**
         * 向管道输入流写入一个短字符串
         */
        private void writeShortMessage(){
            String info="Write a short message.";
            try {
                out.write(info.getBytes());
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * 向管道输出流写入一个长字符串
         */
        private void writeLongMessage(){

            StringBuffer sb=new StringBuffer();
            //写入1020个字符
            for(int i=0;i<102;i++){
                sb.append("0123456789");
            }
            //写入26个字符
            sb.append("abcdefghijklmnopqrstuvwxyz");
            //str的长度为1046
            String str=sb.toString();

            try {
                out.write(str.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 管道输入流测试
     */
    public class Receiver extends Thread{

        private PipedInputStream in=new PipedInputStream();
        // 获得“管道输入流”对象
        public PipedInputStream getInputStream(){
            return in;
        }
        @Override
        public void run() {
            //readMessageOnce();
            //读取超过1024个字节的数据
            readMessageContinued();
        }
        /**
         * 从管道输入流中读取1024个字节的数据
         */
        public void readMessageOnce(){
            byte[] buf=new byte[2048];
            try{
                //将管道输入流的数据写入到byte数组，由于管道输入流默认只有1024字节，所以只会写入1024个字节的数据
                int len=in.read(buf);
                System.out.println(new String(buf,0,len));
                in.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }

        public void readMessageContinued(){
            int total=0;

            while(true) {
                byte[] buf = new byte[1024];
                try {
                    int len = in.read(buf);
                    total += len;
                    System.out.println(new String(buf,0,len));
                    // 若读取到1024+个字节后，结束循环
                    if (total > 1024)
                        break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
